#include <iostream>
#include <math.h>
#include <atlimage.h>
#include <gl/glut.h>
int width = 800, height = 600;


#define MAXVERT 100
typedef struct tagVERT {
	int NumVert;
	int Vert[MAXVERT][2];
} VERT;

VERT v = {0};

GLbyte *MyLoadImage(const wchar_t *szFileName, GLint *iWidth, GLint *iHeight)
{
	int M, N, x, y, k;
	GLbyte	*pBits;
	COLORREF color;
	GLbyte  R, G, B;
	CImage image;
	image.Load(szFileName);
	N = image.GetWidth();
	M = image.GetHeight();
	pBits = (GLbyte *)calloc(M*N*3,sizeof(GLbyte));
	for (x=0; x<M; x++)
		for (y=0; y<N; y++) {
			color = image.GetPixel(y,x);
			R = GetRValue(color);
			G = GetGValue(color);
			B = GetBValue(color);
			k = (x*N+y)*3;
			pBits[k]   = B;
			pBits[k+1] = G;
			pBits[k+2] = R;
		}
	image.Destroy();
	*iWidth = N;
	*iHeight = M;
	return pBits;
}

void KhoiTaoDanDecan(void)
{
	// Init mode load image to polygon
	GLbyte *pBytes;
	GLint iWidth, iHeight;

	// Load texture
	// Tao texture
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	pBytes = MyLoadImage(L"usertile20.bmp", &iWidth, &iHeight);
	//pBytes = MyLoadImage(L"WideScreen02.bmp", &iWidth, &iHeight);
	

	
	// Khai bao
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, iWidth, iHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes);
	free(pBytes);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	//glEnable(GL_TEXTURE_2D);

	return;
}


void Init(void)
{
	// Thiet lap mau nen
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

	// Thiet lap phep chieu
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-width/2,width/2,-height/2,height/2);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	KhoiTaoDanDecan();
	return;
}
void dandecaldagiacloi(void)
{
	int xmax,xmin,ymax,ymin;
	xmin =v.Vert[0][0];
	xmax =v.Vert[0][0];
	ymin=v.Vert[0][1];
	ymax=v.Vert[0][1];
	for (int n=1;n<v.NumVert;n++)
	{
		if (v.Vert[n][0] < xmin)
			xmin=v.Vert[n][0];
		if (v.Vert[n][0] > xmax)
			xmax=v.Vert[n][0];
		if (v.Vert[n][1] < ymin)
			ymin=v.Vert[n][1];
		if (v.Vert[n][1] > ymax)
			ymax=v.Vert[n][1];
	}


	float x,y,s, t;
	glColor3f(1,1,1);
	glEnable(GL_TEXTURE_2D);
	glBegin(GL_POLYGON);
	for (int n=0; n<v.NumVert;n++)
	{
		x= v.Vert[n][0];
		y = v.Vert[n][1];
		s=(x-xmin)/(xmax-xmin);
		t=(y-ymin)/(ymax-ymin);
		glTexCoord2f(s,t);
		glVertex2f(x,y);
	}
	glEnd();
	glDisable(GL_TEXTURE_2D);
return;
}



void ProcessMouse(int button, int state, int x, int y)
{
	if (button == GLUT_LEFT_BUTTON) {
		if (state == GLUT_DOWN) {
			glColor3f(1,0,0);
			glPointSize(5);
			glBegin(GL_POINTS);
			x = x - width/2;
			y = height/2 - y;
			v.Vert[v.NumVert][0] = x;
			v.Vert[v.NumVert][1] = y;
			v.NumVert++;
			glVertex2i(x,y);
			glEnd();
			glFlush();
		}
	}
	else if (button == GLUT_RIGHT_BUTTON) {
		glClear(GL_COLOR_BUFFER_BIT);
		dandecaldagiacloi();
		glFlush();
	}
	return;
}



void ComputersGraphics(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glutCreateWindow("Computers Graphics With OpenGL");
				glutMouseFunc(ProcessMouse);
glutCreateWindow("Computers Graphics With OpenGL");
				glutMouseFunc(ProcessMouse);
glutCreateWindow("Computers Graphics With OpenGL");
				glutMouseFunc(ProcessMouse);
	glFlush();
	return;
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(10,10);
	glutInitWindowSize(width,height);
	glutCreateWindow("Computers Graphics With OpenGL");
				glutMouseFunc(ProcessMouse);
	Init();
	glutDisplayFunc(ComputersGraphics);
	glutMainLoop();
    return 0;
} 
